package com.coin.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.coin.domain.Notice;
import com.coin.model.R;
import com.coin.service.NoticeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Auther: 李 力
 * @Date: 2025/5/28
 * @Description: 公告管理
 * @version: 1.0
 */
@Api(tags = "公告管理")
@RestController
@RequestMapping("/notices")
public class NoticeController {


    @Autowired
    private NoticeService noticeService;

    /**
     * @Description: 分页查询
     * @Author: 李 力
     * @Date: 2025/5/28
     * @Param: Page<Notice> page, String title, String startTime, String endTime, Integer status
     * @return: R
     */
    @GetMapping
    @ApiOperation(value = "分页查询公告")
    @PreAuthorize("hasAuthority('notice_query')")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"), @ApiImplicitParam(name = "size", value = "每页显示的条数"), @ApiImplicitParam(name = "title", value = "公告的标题"), @ApiImplicitParam(name = "startTime", value = "公告的创建开始时间"), @ApiImplicitParam(name = "endTime", value = "公告的创建结束时间时间"),})
    public R<Page<Notice>> findByPage(@ApiIgnore Page<Notice> page, String title, String startTime, String endTime, Integer status) {
        page.addOrder(OrderItem.desc("last_update_time"));
        Page<Notice> result = noticeService.findByPage(page, title, startTime, endTime, status);
        return R.ok(result);
    }

    /**
     * @Description: 删除公告
     * @Author: 李 力
     * @Date: 2025/5/29
     * @Param: ids
     * @return: R
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除一个公告")
    @PreAuthorize("hasAuthority('notice_delete')")
    @ApiImplicitParams({@ApiImplicitParam(name = "ids", value = "要删除的id的集合")})
    public R delete(@RequestBody String[] ids) {
        boolean result = noticeService.deleteNotices(ids);
        return result ? R.ok("删除成功") : R.fail("删除失败");
    }

    /**
     * @Description: 启用/禁用一个公告
     * @Author: 李 力
     * @Date: 2025/5/29
     * @Param: id  status
     * @return: status
     */
    @PostMapping("/updateStatus")
    @ApiOperation(value = "启用/禁用一个公告")
    @PreAuthorize("hasAuthority('notice_update')")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要启用/禁用一个公告的公告Id"),
            @ApiImplicitParam(name = "status", value = "要设置的公告状态")

    })
    public R updateStatus(Long id, Integer status) {
        boolean result = noticeService.updateNotice(id, status);
        return result ? R.ok("成功") : R.fail("失败");
    }

    /**
     * @Description: 新增一个公告
     * @Author: 李 力
     * @Date: 2025/5/29
     * @Param: notice
     * @return: R
     */
    @PostMapping
    @ApiOperation(value = "新增一个公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice", value = "notice的json数据")
    })
    @PreAuthorize("hasAuthority('notice_create')")
    public R add(@RequestBody @Validated Notice notice) {
        boolean result = noticeService.addNotice(notice);
        return result ? R.ok("新增成功") : R.fail("新增失败");
    }


    /**
     * @Description: 修改公告
     * @Author: 李 力
     * @Date: 2025/5/29
     * @Param: notice
     * @return: R
     */
    @PutMapping
    @ApiOperation(value = "修改一个公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice", value = "notice的json数据")
    })
    @PreAuthorize("hasAuthority('notice_update')")
    public R update(@RequestBody @Validated Notice notice) {
        boolean result = noticeService.modifyNotice(notice);
        return result ? R.ok("修改成功") : R.fail("修改失败");
    }

    /**
     * @Description: 前台公告查询
     * @Author: 李 力
     * @Date: 2025/6/21
     * @Param:
     * @return: R
     */
    @GetMapping("/simple")
    public R<Page<Notice>> simple(Page<Notice> page) {
        Page<Notice> data = noticeService.simple(page);
        return R.ok(data);
    }

    /**
     * @Description: 根据Id查询公告详情
     * @Author: 李 力
     * @Date: 2025/6/21
     * @Param:
     * @return: R
     */
    @GetMapping("/simple/{id}")
    public R<Notice> getSimpleDetail(@PathVariable("id") Long id) {
        Notice notice = noticeService.getSimpleDetail(id);
        return R.ok(notice);
    }

}
